import sqlite3
import datetime
import time

# 交易记录监控工具
def monitor_trade_records():
    print("=== 交易记录监控工具 ===")
    print(f"监控时间: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
    
    try:
        # 连接数据库
        conn = sqlite3.connect('trade_monitor.db')
        cursor = conn.cursor()
        
        # 获取总记录数
        cursor.execute("SELECT COUNT(*) FROM trade_records")
        total_count = cursor.fetchone()[0]
        print(f"总记录数: {total_count}")
        
        # 获取今日记录数
        today = datetime.date.today().strftime('%Y-%m-%d')
        cursor.execute("SELECT COUNT(*) FROM trade_records WHERE timestamp LIKE ?", (f"{today}%",))
        today_count = cursor.fetchone()[0]
        print(f"今日记录数: {today_count}")
        
        # 获取最新的5条记录
        print("\n最新的5条交易记录:")
        cursor.execute("SELECT * FROM trade_records ORDER BY timestamp DESC LIMIT 5")
        latest_records = cursor.fetchall()
        
        for record in latest_records:
            print(f"  {record}")
            
        # 检查是否有交易但没有记录的情况
        if today_count == 0:
            print("\n警告: 今日没有交易记录，可能存在问题！")
        
    except Exception as e:
        print(f"监控过程中出错: {str(e)}")
    finally:
        if conn:
            conn.close()

if __name__ == "__main__":
    monitor_trade_records()
